<?php
/**
 * 
 * Helper to build a list of action links.
 * 
 * @category Lux
 * 
 * @package Lux_View
 * 
 * @author Rodrigo Moraes <rodrigo.moraes@gmail.com>
 * 
 * @license http://opensource.org/licenses/bsd-license.php BSD
 * 
 * @version $Id: ActionList.php 137 2008-09-29 17:47:03Z rodrigo.moraes $
 *
 */
class Lux_View_Helper_ActionList extends Solar_View_Helper
{
    /**
     * 
     * User-defined configuration values.
     * 
     * Keys are...
     * 
     * `list_type`
     * : (string) The type of list to use; default is 'ul'. Only 'ul' and 'ol'
     *   are honored.     
     * 
     * `list_id`
     * : (string) The ID attribute for the list.
     * 
     * `list_class`
     * :(string) The class attribute for the list.
     *
     * `first_class`
     * : (string) The CSS class for the first item in the list.
     *
     * `curr_class`
     * : (string) The CSS class for the currently active item in the list.     
     * 
     * `last_class`
     * : (string) The CSS class for the last item in the list.
     * 
     * @var array
     * 
     */
    protected $_Lux_View_Helper_ActionList = array(
        'list_type'     => 'ul',
        'list_id'       => null,
        'list_class'    => null,
        'first_class'   => 'first',
        'curr_class'    => 'curr',
        'last_class'    => 'last',
    );
    
    /**
     *
     * Returns a list of action links.
     *
     * @param array $items List of items using. See examples for the format:
     
     * {{code: php
     *
     *     $items = array();
     *
     *     // Simple item: create action using the provided locale code. The key
     *     // is used to check if this is the active item.
     *     $items['/path/to/action'] = 'ACTION_LOCALE_CODE';
     *
     *     // Named item: same as above, but using a array for the definition.
     *     // The key doesn't need to be the action, allowing "named" items.
     *     // Optionally, a CSS class name can be passed as a third value.
     *     $items['item-name'] = array(
     *         '/path/to/action',
     *         'ACTION_LOCALE_CODE',
     *         'css-class-for-this-item'
     *     );
     *
     *     // No-action item: just escape and add the content to the list
     *     $items[] = 'some text to add to the list';
     *
     * }}
     *
     * @param string $current The currently "active" item in the list, if any.
     *
     * @param array $config Additional config options to build the list.
     *
     * @return string A HTML list.
     *
     */
    public function actionList($items, $current = null, $config = null)
    {
        if ($config) {
            $config = array_merge($this->_config, (array) $config);
        } else {
            $config = $this->_config;
        }
        
        $attribs = $this->_view->attribs(array(
            'id'    => $config['list_id'],
            'class' => $config['list_class'],
        ));
        
        // make sure we have ol or ul
        $list_type = strtolower($config['list_type']);
        if ($list_type != 'ol') {
            $list_type = 'ul';
        }
        
        // start the list
        $html = "<$list_type$attribs>\n";

        $iter = 1;
        $total = count($items);
        foreach ($items as $key => $spec) {
            // reset class and attributes for this item
            $class   = array();
            $attribs = '';
            
            // add 'first' class?
            if (($iter == 1) && $config['first_class']) {
                $class[] = $config['first_class'];
            }
            
            // add 'last' class?
            if (($iter == $total) && $config['last_class']) {
                $class[] = $config['last_class'];
            }
            
            // add 'current' class?
            if (($key === $current) && $config['curr_class']) {
                $class[] = $config['curr_class'];
            }

            // is this an action item?
            if (is_string($key) || is_array($spec)) {
                // using array for the item info?
                if (is_array($spec)) {
                    // make sure we have always three values in the array
                    $spec = array_pad(array_values($spec), 3, null);
                    
                    // add a special class for this item?
                    if ($spec[2]) {
                        $class[] = $spec[2];
                    }
                    
                    // extract href and text
                    $key  = $spec[0];
                    $spec = $spec[1];
                }
                
                // build the action
                $href = $this->_view->escape($key);
                $text = $this->_view->escape($spec);
                $content = "<a href=\"$href\">$text</a>";
            } else {
                // no action: just escape the content
                $content = $this->_view->escape($spec);
            }
            
            // add any CSS class?
            if (! empty($class)) {
                $attribs = $this->_view->attribs(array(
                    'class' => $class,
                ));
            }
            
            // set the item
            $html .= "    <li$attribs>$content</li>\n";
            $iter++;
        }
        
        // done.
        $html .= "</$list_type>";
        return $html;
    }
}
